package com.iot.manage.dao;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.DeviceEvent;
import com.iot.manage.pojo.SysCode;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DeviceEventMapper {

    @Select("<script>" +
            "SELECT\n" +
            "temp.DEVENT_ID deventId,\n" +
            "temp.DEVENT_CODE deventCode,\n" +
            "temp.DEVENT_NAME deventName,\n" +
            "deventType,\n" +
            "temp.DEVENT_ARG deventArg\n" +
            "FROM ( \n" +
            "SELECT\n" +
            "ROWNUM rowno,\n" +
            "t.*,\n" +
            "c.CODE_NAME deventType\n" +
            "FROM T_DEVICE_EVENT t \n" +
            "LEFT JOIN (SELECT * FROM T_SYS_CODE WHERE CODE_TYPE='事件类型') c ON t.DEVENT_TYPE = c.CODE_VALUE\n" +
            "WHERE (t.DEVENT_NAME LIKE CONCAT(CONCAT('%', #{query}),'%') OR t.DEVENT_CODE LIKE CONCAT(CONCAT('%',#{query}),'%')) AND ROWNUM &lt;= #{pagesize} ORDER BY t.DEVENT_ID ) temp \n" +
            "WHERE temp.rowno &gt; #{start}" +
            "</script>")
    List<DeviceEvent> findDEventList(@Param("query") String query, @Param("start") int start, @Param("pagesize") int pagesize);

    @Select("<script>SELECT COUNT(a.DEVENT_ID) FROM T_DEVICE_EVENT a WHERE a.DEVENT_CODE LIKE CONCAT(CONCAT('%', #{query}),'%') OR a.DEVENT_NAME LIKE CONCAT(CONCAT('%', #{query}),'%')</script>")
    int countDEventList(String query);

    @Insert("<script>" +
            "INSERT INTO T_DEVICE_EVENT \n" +
            "(DEVENT_ID,DEVENT_CODE,DEVENT_NAME,DEVENT_TYPE" +
            "<if test=\" deventArg != null\">"+
            ",DEVENT_ARG" +
            "</if>"+
            ") \n" +
            "VALUES \n" +
            "(#{deventId},#{deventCode},#{deventName},#{deventType}" +
            "<if test=\" deventArg != null\">"+
            ",#{deventArg}" +
            "</if>"+
            ")" +
            "</script>")
    void addDEventInfo(JSONObject params);

    @Select("<script>" +
            "SELECT  \n" +
            " temp.DEVENT_ID deventId,  \n" +
            " temp.DEVENT_CODE deventCode,  \n" +
            " temp.DEVENT_NAME deventName,\n" +
            " temp.DEVENT_TYPE deventType,\n" +
            " temp.DEVENT_ARG deventArg\n" +
            "FROM T_DEVICE_EVENT temp\n" +
            "\tWHERE temp.DEVENT_ID = #{params} OR temp.DEVENT_CODE = #{params}\n" +
            "</script>")
    DeviceEvent findDEventInfoByIdOrCode(String params);

    @Update("<script>" +
            "UPDATE T_DEVICE_EVENT SET \n" +
            "\tDEVENT_CODE = #{deventCode},\n" +
            "\tDEVENT_NAME = #{deventName},\n" +
            "\tDEVENT_TYPE = #{deventType}\n" +
            "<if test=\" deventArg != null\">"+
            "\t,DEVENT_ARG = #{deventArg}\n" +
            "</if>"+
            "WHERE DEVENT_ID = #{deventId}" +
            "</script>")
    void updateDEventInfo(JSONObject params);

    @Delete("<script>DELETE FROM T_DEVICE_EVENT WHERE DEVENT_ID = #{deventId}</script>")
    void deleteDEventInfo(String deventId);

    @Select("<script>SELECT a.CODE_VALUE codeValue, a.CODE_NAME codeName FROM T_SYS_CODE a WHERE a.CODE_TYPE='事件类型' ORDER BY a.CODE_VALUE</script>")
    List<SysCode> findDEventTypeList();

    @Select("<script>SELECT count(MODEL_ID) FROM T_DEVICE_MODEL WHERE MODEL_ARG LIKE CONCAT(CONCAT('%', CONCAT(CONCAT('\"', #{query}),'\"')),'%')</script>")
    int countEventUseTotal(String deventCode);

    @Update("<script>update T_DEVICE_MODEL set MODEL_ARG = replace(MODEL_ARG,CONCAT(CONCAT('\"', #{oldDEventCode}),'\"'),CONCAT(CONCAT('\"', #{deventCode}),'\"'))</script>")
    void updateDeviceModelArg(JSONObject params);
}
